package com.siyuanren.gcocp.common.util;

import com.siyuanren.gcocp.enums.wx.BaseEnum;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author guwr
 * @date 2018/5/11
 */
public class EnumUtil implements Serializable {


    /**
     * 获取枚举类子集对应描述
     */
    public static <T extends BaseEnum> String getEnumDesc(int value, Class<T> clazz) {
        T anEnum = getEnum(value, clazz);
        if (anEnum != null) {
            return anEnum.getDesc();
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取枚举类
     */
    public static <T extends BaseEnum> T getEnum(int value, Class<T> clazz) {
        for (T t : clazz.getEnumConstants()) {
            if (Objects.equals(t.getValue(), value)) {
                return t;
            }
        }
        return null;
    }
}
